//
//  SelecterToolsScrolView.m
//  SelecterTools
//
//  Created by zhao on 16/3/15.
//  Copyright © 2016年 xincheng. All rights reserved.
//

#import "SelecterToolsScrolView.h"


@interface SelecterToolsScrolView()<UIScrollViewDelegate>

@property(nonatomic,copy)BtnClick btnClick;

@property (nonatomic,strong)UIFont *norFont;
@property (nonatomic,strong)UIFont *selFont;

@property(nonatomic,retain)NSMutableArray *btnArr;

@property(nonatomic,retain)UIButton *previousBtn;
@property(nonatomic,retain)UIButton *currentBtn;


@property(nonatomic,retain)UIView *bottomScrLine;

@end

@implementation SelecterToolsScrolView

-(instancetype)initWithFrame:(CGRect)frame bottomLineSpace:(CGFloat)bottomLineSpace bottomLineSize:(CGSize)bottomLineSize radius:(CGFloat)radius lineColor:(UIColor *)lineColor isShowMiddleLine:(BOOL)isShowMiddleLine titleArr:(NSArray *)titleArr font:(CGFloat)font normalColor:(UIColor *)normalColor selectedColor:(UIColor *)selectedColor andBtnBlock:(BtnClick)btnClick
{
    if (self = [super initWithFrame:frame]) {
        self = [self initWithFrame:frame bottomLineSpace:bottomLineSpace bottomLineSize:bottomLineSize radius:radius lineColor:lineColor isShowMiddleLine:isShowMiddleLine titleArr:titleArr font:HHFont(font) selFont:HHFont(font) normalColor:normalColor selectedColor:selectedColor andBtnBlock:btnClick];
    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame bottomLineSpace:(CGFloat)bottomLineSpace bottomLineSize:(CGSize)bottomLineSize radius:(CGFloat)radius lineColor:(UIColor *)lineColor isShowMiddleLine:(BOOL)isShowMiddleLine titleArr:(NSArray *)titleArr font:(UIFont *)font selFont:(UIFont *)selFont normalColor:(UIColor *)normalColor selectedColor:(UIColor *)selectedColor andBtnBlock:(BtnClick)btnClick
{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = HHColorHexFFFFFF;
        
        CGFloat Frame_Width = frame.size.width;
        CGFloat Frame_Height = frame.size.height;
        CGFloat lineWidth = bottomLineSize.width;
        CGFloat lineHeight = bottomLineSize.height;
        
        self.btnClick = btnClick;
        self.norFont = font;
        self.selFont = selFont;
        
        
        CGFloat allTitleWidth = 0.0;
        NSMutableArray *allTitleWidthArr = [NSMutableArray array];
        for (int i = 0; i<titleArr.count; i++) {
            CGFloat titleWidth = [titleArr[i] sizeWithFont:selFont maxWidth:ScreenWidth].width;
            
            allTitleWidth = allTitleWidth + titleWidth;
            [allTitleWidthArr addObject:[NSNumber numberWithFloat:titleWidth]];
        }
        
        CGFloat spaceWidth = 10.0;
        if (Frame_Width > allTitleWidth) {
            spaceWidth = (Frame_Width - allTitleWidth) / titleArr.count / 2.0;
        }
        
        _btnArr = [NSMutableArray array];
        CGFloat ws = 0;
        for (int i = 0; i<titleArr.count; i++) {
            if (i) {
                ws = ws + [allTitleWidthArr[i - 1] floatValue] + 2 * spaceWidth;
            }else{
                ws = spaceWidth;
            }
            UIButton *titleBtn = [[UIButton alloc]initWithFrame:CGRectMake(ws , 0, [allTitleWidthArr[i] floatValue], Frame_Height)];
            
            [titleBtn setTitle:titleArr[i] forState:UIControlStateNormal];
            titleBtn.titleLabel.font = font;
            [titleBtn setTitleColor:normalColor forState:UIControlStateNormal];
            [titleBtn setTitleColor:selectedColor forState:UIControlStateSelected];
            
            titleBtn.tag = i;
            [titleBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:titleBtn];
            [_btnArr addObject:titleBtn];
            if (i == 0) {
                _previousBtn = titleBtn;
                _currentBtn = titleBtn;
                titleBtn.titleLabel.font = selFont;
                titleBtn.selected = YES;
            }
            
            if (isShowMiddleLine && i) {
                UIView *lineV = [[UIView alloc]initWithFrame:CGRectMake(ws - spaceWidth - 1, (Frame_Height - 13) / 2.0, 2.0, 13.0)];
                lineV.backgroundColor = HHColorHexEEEEEE;
                [self addSubview:lineV];
            }
        }
        
        self.bottomScrLine = [[UIView alloc]initWithFrame:CGRectMake(0, Frame_Height - bottomLineSpace, lineWidth , lineHeight)];
        self.bottomScrLine.center = CGPointMake(_currentBtn.center.x, self.bottomScrLine.center.y);
        self.bottomScrLine.backgroundColor = lineColor;
        if (radius > 0) {
            self.bottomScrLine.layer.cornerRadius = radius;
            self.bottomScrLine.layer.masksToBounds = NO;
        }
        [self addSubview:_bottomScrLine];
        
        self.delegate = self;
        self.contentSize = CGSizeMake(spaceWidth * 2 * titleArr.count + allTitleWidth, Frame_Height);
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
//        self.layer.borderWidth = 0.5;
//        self.layer.borderColor = [UIColor lightGrayColor].CGColor;
        
    }
    return self;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.y) {
        scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x, 0);
    }
}

-(void)updateSelecterToolsIndex:(NSInteger )index
{
    UIButton *selectBtn = _btnArr[index];
    [self changeSelectBtn:selectBtn];
}

-(void)btnClick:(UIButton *)sender
{
    self.btnClick(sender);
}

-(void)changeSelectBtn:(UIButton *)btn
{
    
    _previousBtn = _currentBtn;
    _currentBtn = btn;
    _previousBtn.selected = NO;
    _currentBtn.selected = YES;
    
    _previousBtn.titleLabel.font = self.norFont;
    _currentBtn.titleLabel.font = self.selFont;

    
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
//        _bottomScrLine.mj_w = _currentBtn.mj_w;
        self.bottomScrLine.center = CGPointMake(self.currentBtn.center.x, self.bottomScrLine.center.y);
    } completion:^(BOOL finished) {
        
    }];
    
    CGFloat Frame_Width = self.frame.size.width;
    CGFloat ContentV_Frame_Width = self.contentSize.width;

    
    if (Frame_Width < ContentV_Frame_Width) {
        if (self.currentBtn.center.x <= Frame_Width/2.0) {
            [self setContentOffset:CGPointMake(0, 0) animated:YES];
    

        }else{
            if (ContentV_Frame_Width - self.currentBtn.center.x >= Frame_Width/2.0) {
                [self setContentOffset:CGPointMake(self.currentBtn.center.x - Frame_Width/2.0, 0) animated:YES];
    

            }else{
                [self setContentOffset:CGPointMake(ContentV_Frame_Width - Frame_Width, 0) animated:YES];
    

            }
        }
    }

}

@end








@interface LoginSelecterToolsScrolView()<UIScrollViewDelegate>

@property(nonatomic,copy)BtnClick btnClick;

@property (nonatomic,strong)UIFont *norFont;
@property (nonatomic,strong)UIFont *selFont;


@property(nonatomic,strong)NSMutableArray *btnArr;

@property(nonatomic,strong)UIButton *previousBtn;
@property(nonatomic,strong)UIButton *currentBtn;

@property(nonatomic,strong)NSMutableArray *norTitleWidthArr;
@property(nonatomic,strong)NSMutableArray *selTitleWidthArr;

@property (nonatomic, assign)CGFloat spaceWidth;

@property (nonatomic,strong)UIImageView *bottomImageV;

@property (nonatomic, assign)CGFloat bottomWidth;
@property (nonatomic, assign)CGFloat bottomHeight;
@property (nonatomic, assign)CGFloat bottomCenterY;

@end

@implementation LoginSelecterToolsScrolView

-(instancetype)initWithFrame:(CGRect)frame titleArr:(NSArray *)titleArr bottomImage:(NSString *)bottomImage norFont:(UIFont *)norFont selFont:(UIFont *)selFont bgColor:(UIColor *)bgColor normalColor:(UIColor *)normalColor selectedColor:(UIColor *)selectedColor andBtnBlock:(BtnClick)btnClick
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = bgColor;
        CGFloat Frame_Width = frame.size.width;
        CGFloat Frame_Height = frame.size.height;
        
        self.btnClick = btnClick;
        self.norFont = norFont;
        self.selFont = selFont;
        self.bottomWidth = 56;
        self.bottomHeight = 12;
        self.bottomCenterY = Frame_Height - self.bottomHeight/2.0 - 5.0;
        
        self.norTitleWidthArr = [NSMutableArray array];
        self.selTitleWidthArr = [NSMutableArray array];
        CGFloat allTitleWidth = 0.0;
        for (int i = 0; i<titleArr.count; i++) {
            CGFloat norTitleWidth = [titleArr[i] sizeWithFont:norFont maxWidth:ScreenWidth].width;
            CGFloat selTitleWidth = [titleArr[i] sizeWithFont:selFont maxWidth:ScreenWidth].width;
            
            [self.norTitleWidthArr addObject:[NSNumber numberWithFloat:norTitleWidth]];
            [self.selTitleWidthArr addObject:[NSNumber numberWithFloat:selTitleWidth]];
            
            allTitleWidth = allTitleWidth + (i == 0 ? selTitleWidth : norTitleWidth);
        }
        
        CGFloat spaceWidth = 15.0;
        if (Frame_Width > allTitleWidth) {
            spaceWidth = (Frame_Width - allTitleWidth) / titleArr.count / 2.0;
        }
        self.spaceWidth = spaceWidth;
        
        _btnArr = [NSMutableArray array];

        for (int i = 0; i<titleArr.count; i++) {
            UIButton *titleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [titleBtn setTitle:titleArr[i] forState:UIControlStateNormal];
            titleBtn.titleLabel.font = norFont;
            [titleBtn setTitleColor:normalColor forState:UIControlStateNormal];
            [titleBtn setTitleColor:selectedColor forState:UIControlStateSelected];
            
            titleBtn.tag = i;
            [titleBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    

            [self addSubview:titleBtn];
            [_btnArr addObject:titleBtn];
            if (i == 0) {
                titleBtn.titleLabel.font = selFont;
                _previousBtn = titleBtn;
                _currentBtn = titleBtn;
                titleBtn.selected = YES;
            }
        }
        //更新所有button的frame
        [self updateBtnsFrameWithIndex:0];
        
        self.bottomImageV = [[UIImageView alloc]initWithFrame:CGRectMake(0, self.bottomCenterY - 3.5, self.bottomWidth, self.bottomHeight)];
        [self addSubview:self.bottomImageV];
        [self.bottomImageV setImage:[UIImage imageNamed:bottomImage]];
        self.bottomImageV.contentMode = UIViewContentModeCenter;
        self.bottomImageV.center = CGPointMake(_currentBtn.center.x, self.bottomCenterY);
        
        self.delegate = self;
        self.contentSize = CGSizeMake(spaceWidth * 2 * titleArr.count + allTitleWidth, Frame_Height);
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
    }
    return self;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.y) {
        scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x, 0);
    }
}

-(void)btnClick:(UIButton *)sender
{
    [self updateSelecterToolsIndex:sender.tag];
    self.btnClick(sender);
}



-(void)updateSelecterToolsIndex:(NSInteger )index
{
    UIButton *selectBtn = _btnArr[index];

    _previousBtn = _currentBtn;
    _currentBtn = selectBtn;
    _previousBtn.selected = NO;
    _currentBtn.selected = YES;
    _previousBtn.titleLabel.font = self.norFont;
    _currentBtn.titleLabel.font = self.selFont;
    
    //更新所有button的frame
    [self updateBtnsFrameWithIndex:index];
    
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        self.bottomImageV.center = CGPointMake(self.currentBtn.center.x,self.bottomCenterY);
    } completion:^(BOOL finished) {
    }];
    
    CGFloat Frame_Width = self.frame.size.width;
    CGFloat ContentV_Frame_Width = self.contentSize.width;
    
    
    if (Frame_Width < ContentV_Frame_Width) {
        if (self.currentBtn.center.x <= Frame_Width/2.0) {
            [self setContentOffset:CGPointMake(0, 0) animated:YES];
        }else{
            if (ContentV_Frame_Width - self.currentBtn.center.x >= Frame_Width/2.0) {
                [self setContentOffset:CGPointMake(self.currentBtn.center.x - Frame_Width/2.0, 0) animated:YES];
            }else{
                [self setContentOffset:CGPointMake(ContentV_Frame_Width - Frame_Width, 0) animated:YES];
            }
        }
    }
    
}


//更新所有button的frame
- (void)updateBtnsFrameWithIndex:(NSInteger )index{
    CGFloat ws = 0;
    for (int i = 0; i < self.btnArr.count; i++) {
        UIButton *titleBtn = self.btnArr[i];
        if (i) {
            CGFloat lastBtnWidth = (self.currentBtn.tag == i - 1) ? [self.selTitleWidthArr[i - 1] floatValue] : [self.norTitleWidthArr[i - 1] floatValue];
            ws = ws + lastBtnWidth + 2 * self.spaceWidth;
        }else{
            ws = self.spaceWidth;
        }
        
        CGFloat currentBtnWidth = (self.currentBtn.tag == i) ? [self.selTitleWidthArr[i] floatValue] : [self.norTitleWidthArr[i] floatValue];
        titleBtn.frame = CGRectMake(ws , 0, currentBtnWidth, self.frame.size.height - 2);
    }
}


@end











@interface HomeSelecterToolsScrolView()<UIScrollViewDelegate>

@property(nonatomic,copy)BtnClick btnClick;

@property (nonatomic,strong)UIFont *norFont;
@property (nonatomic,strong)UIFont *selFont;


@property(nonatomic,strong)NSMutableArray *btnArr;

@property(nonatomic,strong)UIButton *previousBtn;
@property(nonatomic,strong)UIButton *currentBtn;

@property(nonatomic,strong)NSMutableArray *norTitleWidthArr;
@property(nonatomic,strong)NSMutableArray *selTitleWidthArr;

@property (nonatomic, assign)CGFloat spaceWidth;

@property (nonatomic,strong)UIImageView *bottomImageV;

@property (nonatomic, assign)CGFloat bottomWidth;
@property (nonatomic, assign)CGFloat bottomHeight;
@property (nonatomic, assign)CGFloat bottomCenterY;

@end

@implementation HomeSelecterToolsScrolView

-(instancetype)initWithFrame:(CGRect)frame titleArr:(NSArray *)titleArr bottomImage:(NSString *)bottomImage norFont:(UIFont *)norFont selFont:(UIFont *)selFont andBtnBlock:(BtnClick)btnClick{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = HHColorHexFFFFFF;
        CGFloat Frame_Width = frame.size.width;
        CGFloat Frame_Height = frame.size.height;
        
        self.btnClick = btnClick;
        self.norFont = norFont;
        self.selFont = selFont;
        self.bottomWidth = 56;
        self.bottomHeight = 12;
        self.bottomCenterY = Frame_Height - self.bottomHeight/2.0 - 5.0;
        
        self.norTitleWidthArr = [NSMutableArray array];
        self.selTitleWidthArr = [NSMutableArray array];
        CGFloat allTitleWidth = 0.0;
        for (int i = 0; i<titleArr.count; i++) {
            CGFloat norTitleWidth = [titleArr[i] sizeWithFont:norFont maxWidth:ScreenWidth].width;
            CGFloat selTitleWidth = [titleArr[i] sizeWithFont:selFont maxWidth:ScreenWidth].width;
            
            [self.norTitleWidthArr addObject:[NSNumber numberWithFloat:norTitleWidth]];
            [self.selTitleWidthArr addObject:[NSNumber numberWithFloat:selTitleWidth]];
            
            allTitleWidth = allTitleWidth + (i == 0 ? selTitleWidth : norTitleWidth);
        }
        
        CGFloat spaceWidth = 15.0;
        if (Frame_Width > allTitleWidth) {
            spaceWidth = (Frame_Width - allTitleWidth) / titleArr.count / 2.0;
        }
        self.spaceWidth = spaceWidth;
        
        _btnArr = [NSMutableArray array];

        for (int i = 0; i<titleArr.count; i++) {
            UIButton *titleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [titleBtn setTitle:titleArr[i] forState:UIControlStateNormal];
            titleBtn.titleLabel.font = norFont;
            [titleBtn setTitleColor:HHColorHex979797 forState:UIControlStateNormal];
            [titleBtn setTitleColor:HHColorHex343434 forState:UIControlStateSelected];
            
            titleBtn.tag = i;
            [titleBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:titleBtn];
            [_btnArr addObject:titleBtn];
            if (i == 0) {
                titleBtn.titleLabel.font = selFont;
                _previousBtn = titleBtn;
                _currentBtn = titleBtn;
                titleBtn.selected = YES;
            }
        }
        //更新所有button的frame
        [self updateBtnsFrameWithIndex:0];
        
        self.bottomImageV = [[UIImageView alloc]initWithFrame:CGRectMake(0, self.bottomCenterY - 3.5, self.bottomWidth, self.bottomHeight)];
        [self addSubview:self.bottomImageV];
        [self.bottomImageV setImage:[UIImage imageNamed:bottomImage]];
        self.bottomImageV.contentMode = UIViewContentModeCenter;
        self.bottomImageV.center = CGPointMake(_currentBtn.center.x, self.bottomCenterY);
        
        self.delegate = self;
        self.contentSize = CGSizeMake(spaceWidth * 2 * titleArr.count + allTitleWidth, Frame_Height);
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
    }
    return self;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.y) {
        scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x, 0);
    }
}

-(void)btnClick:(UIButton *)sender
{
    [self updateSelecterToolsIndex:sender.tag];
    self.btnClick(sender);
}



-(void)updateSelecterToolsIndex:(NSInteger )index
{
    UIButton *selectBtn = _btnArr[index];

    _previousBtn = _currentBtn;
    _currentBtn = selectBtn;
    _previousBtn.selected = NO;
    _currentBtn.selected = YES;
    _previousBtn.titleLabel.font = self.norFont;
    _currentBtn.titleLabel.font = self.selFont;
    
    //更新所有button的frame
    [self updateBtnsFrameWithIndex:index];
    
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        self.bottomImageV.center = CGPointMake(self.currentBtn.center.x,self.bottomCenterY);
    } completion:^(BOOL finished) {
    }];
    
    CGFloat Frame_Width = self.frame.size.width;
    CGFloat ContentV_Frame_Width = self.contentSize.width;
    
    
    if (Frame_Width < ContentV_Frame_Width) {
        if (self.currentBtn.center.x <= Frame_Width/2.0) {
            [self setContentOffset:CGPointMake(0, 0) animated:YES];
        }else{
            if (ContentV_Frame_Width - self.currentBtn.center.x >= Frame_Width/2.0) {
                [self setContentOffset:CGPointMake(self.currentBtn.center.x - Frame_Width/2.0, 0) animated:YES];
            }else{
                [self setContentOffset:CGPointMake(ContentV_Frame_Width - Frame_Width, 0) animated:YES];
            }
        }
    }
    
}


//更新所有button的frame
- (void)updateBtnsFrameWithIndex:(NSInteger )index{
    CGFloat ws = 0;
    for (int i = 0; i < self.btnArr.count; i++) {
        UIButton *titleBtn = self.btnArr[i];
        if (i) {
            CGFloat lastBtnWidth = (self.currentBtn.tag == i - 1) ? [self.selTitleWidthArr[i - 1] floatValue] : [self.norTitleWidthArr[i - 1] floatValue];
            ws = ws + lastBtnWidth + 2 * self.spaceWidth;
        }else{
            ws = self.spaceWidth;
        }
        
        CGFloat currentBtnWidth = (self.currentBtn.tag == i) ? [self.selTitleWidthArr[i] floatValue] : [self.norTitleWidthArr[i] floatValue];
        titleBtn.frame = CGRectMake(ws , 0, currentBtnWidth, self.frame.size.height - 2);
    }
}


@end



